//
//  NestTableViewController.m
//  Tools
//
//  Created by han on 2019/1/14.
//  Copyright © 2019 han. All rights reserved.
//

#import "NestTableViewController.h"

@interface NestTableViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

//scrollView 滚动时机: 是否允许下滑
@property (nonatomic,assign) BOOL scrollEnable;
@property (weak, nonatomic) IBOutlet UIView *titleView;

@end

@implementation NestTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollEnable = YES;
    // 注意：
    // 1. navigationbar是否透明 2. tableview的高度= scrollview的高度 - 悬浮view的高度
    self.navigationController.navigationBar.translucent = NO;
}
#pragma mark --- scrollView delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isKindOfClass:[UITableView class]]) {
        // tableView
        if (scrollView.contentOffset.y <= 0) {
            //tableView滚动到头,tableview不滚动，scrollview滚动
            scrollView.contentOffset = CGPointZero;
            self.scrollEnable = YES;
        }else { // tableview 向上滚动
            CGRect titleViewRect = [self.titleView convertRect:self.titleView.bounds toView:self.view];
            if (self.scrollEnable && titleViewRect.origin.y > 0) {
                // 当scrollview的titleView还没有滚出去，tableView不滚动
                scrollView.contentOffset = CGPointZero;
                self.scrollEnable = YES;
            }else {
                self.scrollEnable = NO;
            }
        }
    }else {
        // scrollview
        if (!self.scrollEnable) { //当scrollview不能滚动的时候，设置offset高度为header消失的高度
            scrollView.contentOffset = CGPointMake(0, 200);
        }
    }
}

#pragma mark --- tableView delegate & dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NestTableCellIdentifier" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%zd 行，%zd 列",indexPath.section,indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
